Option Strict Off
Option Explicit On
Module UserInput
	Public Sub Undo()
		Dim Return1 As Boolean
		Dim FrameDelID As String 'The frame number to delete as a string
		Dim FileToDelete As String 'The filename of the frame to delete
		If FrameNum < 1 Then
			Call MsgBox("No frames captured!", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Sub
		End If
		If FrameNum = 1 Then
			Call MsgBox("I cannot delete the first frame", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Sub
		End If
		If MsgBox("Do you want to delete the last frame?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Helium Frog Animator") = MsgBoxResult.No Then Exit Sub
		
		Call RemoveDShowPlayBack() 'Remove the video from the playback window to free the .avi file for writing
		If DeleteLastFrame(SavePath & ProjName & ".avi") = False Then
			FunctionOK = OpenDShowPlayBack(SavePath & ProjName & ".avi", (frmCapture.picPlayBack)) 'Load the video back in the playback window
			Call MsgBox("I failed to delete the last frame", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Sub
		Else
			FunctionOK = OpenDShowPlayBack(SavePath & ProjName & ".avi", (frmCapture.picPlayBack)) 'Load the video back in the playback window
			If WindowAtFront = "PlayBack" Then PlayBackMediaPosition.CurrentPosition = PlayBackMediaPosition.Duration 'If in playback, show the last frame of shortened animantion
			If ExtractSingleFrame(SavePath & ProjName & ".avi", -999, My.Application.Info.DirectoryPath & "\Undo.jpg") = True Then 'Extract the last frame available for onion skin
				frmAveraging.picOnion1.Image = System.Drawing.Image.FromFile(My.Application.Info.DirectoryPath & "\Undo.jpg") 'Load the picture into the onion buffer
				'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				If Dir(My.Application.Info.DirectoryPath & "\Undo.jpg") <> "" Then Kill((My.Application.Info.DirectoryPath & "\Undo.jpg")) 'Erase the temporary Undo frame
			Else
				Call MsgBox("I failed to extract the last frame for the onion skin routine", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			End If
			'  Call MsgBox("Successfully deleted the last frame", vbOKOnly + vbExclamation, "Helium Frog Animator")
			
			
			'Now look for individual frames to delete
			FrameDelID = LTrim(Str(FrameNum))
			If Len(FrameDelID) = 1 Then FrameDelID = "00000" & FrameDelID
			If Len(FrameDelID) = 2 Then FrameDelID = "0000" & FrameDelID
			If Len(FrameDelID) = 3 Then FrameDelID = "000" & FrameDelID
			If Len(FrameDelID) = 4 Then FrameDelID = "00" & FrameDelID
			If Len(FrameDelID) = 5 Then FrameDelID = "0" & FrameDelID
			
			FileToDelete = SavePath & ProjName & "_Frame_" & FrameDelID & ".bmp" 'Construct the filename
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Dir(FileToDelete) <> "" Then Kill((FileToDelete)) 'Delete it if it exists.
			
			FileToDelete = SavePath & ProjName & "_Frame_" & FrameDelID & ".gif" 'Construct the filename
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Dir(FileToDelete) <> "" Then Kill((FileToDelete)) 'Delete it if it exists.
			
			FileToDelete = SavePath & ProjName & "_Frame_" & FrameDelID & ".jpg" 'Construct the filename
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Dir(FileToDelete) <> "" Then Kill((FileToDelete)) 'Delete it if it exists.
			
			FileToDelete = SavePath & ProjName & "_Frame_" & FrameDelID & ".png" 'Construct the filename
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Dir(FileToDelete) <> "" Then Kill((FileToDelete)) 'Delete it if it exists.
			
			FileToDelete = SavePath & ProjName & "_Frame_" & FrameDelID & ".tif" 'Construct the filename
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Dir(FileToDelete) <> "" Then Kill((FileToDelete)) 'Delete it if it exists.
			
			FrameNum = FrameNum - 1 'decrease the frame count by 1
		End If
		
		If XSheetActiveYesNo = True Then 'if the exposure sheet is showing move it back it
			ExposureTopPos = FrameNum 'set the top position of the exposure sheet to the frame
			Call frmXSheet.ReloadExposureSheet(ExposureTopPos)
		End If
		
		
		
		
	End Sub
	
	Public Sub RotoScope()
		If WindowAtFront = "PlayBack" Then Call Toggle() 'The program is in playback mode, so toggle to live first
		
		frmRotoscope.Visible = True
		RotoScopeActiveYesNo = True
	End Sub
	
	Public Sub TextWind()
		
	End Sub
	Public Sub Toggle() 'Enables the user to Toggle between playback and live windows.
		If WindowAtFront = "Live" Then
			'Switch to playback window
			If FrameNum < 1 Then Exit Sub 'There are no frames captured, so dont flip to the playback window
			
			
			frmCapture.picPlayBack.Visible = True
			frmCapture.picLive.Visible = False
			WindowAtFront = "PlayBack" 'set variable that Playback window is at the front
			Call frmAnimator.PositionFrameSlider(1) 'Adjust the position of the frame slider so it is correct
			
			Call frmXSheet.ReloadExposureSheet(Fix((PlayBackMediaPosition.CurrentPosition * FPS) + 0.00001)) 'make sure the exposure sheet shows correctly
		Else
			'Switch to Live window
			PlayBackMediaControl.Pause()
			Call AudioPlayer_Stop() 'Stop the soundtrack playing
			PlayStatus = "Stopped"
			
			frmCapture.picLive.Visible = True
			frmCapture.picPlayBack.Visible = False
			WindowAtFront = "Live" 'Set the variable that live window is at the front
		End If
		
	End Sub
	
	
	Public Sub OnionButt()
		If FrameNum < 1 Then Exit Sub 'There are no frames captured, onion skinning is disabled
		If WindowAtFront = "PlayBack" Then Call Toggle() 'The program is in playback mode, so toggle to live mode before switchin onion skinning
		OnionMode = OnionMode * -1 'toggle the onion skinning on or off
	End Sub
	
	Public Sub Settings()
		If WindowAtFront = "PlayBack" Then Call Toggle() 'The program is in playback mode, so toggle to live first
		Call frmSettings.SetUpSettingsBoxContents()
		frmSettings.Visible = True 'Show the settings window
		frmSettings.Refresh()
		ActiveWindowIs = "SettingsWindow" 'make settings window active...program now knows to disable button box and hotkeys
		
	End Sub
	Public Sub Utilities()
		If WindowAtFront = "PlayBack" Then Call Toggle() 'The program is in playback mode, so toggle to live first
		frmUtilities2.Visible = True
		ActiveWindowIs = "UtilitiesWindow"
		
	End Sub
	Public Sub Editor()
		If FrameNum < 1 Then 'There are no frames captured, so you cannot enter the editor
			Call MsgBox("You haven't captured any frames to edit!", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Sub
		End If
		If WindowAtFront = "PlayBack" Then Call Toggle() 'The program is in playback mode, so toggle to live first
		frmEditor.Visible = True 'Show the editor Window
		Call frmEditor.LoadAnimationToEditor() 'load the animation to the editor
		frmEditor.Refresh()
		ActiveWindowIs = "EditorWindow" 'Make editor window active...program now knows to disable button box and hotkeys
	End Sub
	
	Public Sub ToStart()
		If FrameNum < 1 Then Exit Sub 'No frames have been captured, so take no action
		If WindowAtFront = "Live" Then Call Toggle() 'The live window is showing, so to flip to the playback window
		Call AudioPlayer_Stop() 'Stop playing the soundtrack
		
		PlayBackMediaPosition.CurrentPosition = 0
		PlayBackMediaControl.Pause()
		PlayStatus = "Stopped"
		Call frmAnimator.PositionFrameSlider(1) 'Adjust the position of the frame slider so it is correct
	End Sub
	Public Sub Back1()
		If FrameNum < 1 Then Exit Sub 'No frames have been captured, so take no action
		If WindowAtFront = "Live" Then Call Toggle() 'The live window is showing, so to flip to the playback window
		Call AudioPlayer_Stop() 'Stop playing the soundtrack
		PlayBackMediaControl.Pause()
		PlayStatus = "Stopped"
		If PlayBackMediaPosition.CurrentPosition - (1 / FPS) < 0 Then 'Check if this will take it back beyond the start of the .avi
			PlayBackMediaPosition.CurrentPosition = 0 'Take it as far back as possible
			Exit Sub
		Else
			PlayBackMediaPosition.CurrentPosition = PlayBackMediaPosition.CurrentPosition - (1 / FPS)
			PlayBackMediaControl.Pause()
		End If
		Call frmAnimator.PositionFrameSlider(1) 'Adjust the position of the frame slider so it is correct
		'  Call AudioPlayer_Sync                                              'Syncronise the soundtrack to the new position
	End Sub
	Public Sub StopButt()
		If FrameNum < 1 Then Exit Sub 'No frames have been captured, so take no action
		If WindowAtFront = "Live" Then Call Toggle() 'The live window is showing, so to flip to the playback window
		Call AudioPlayer_Stop() 'Stop playing the soundtrack
		
		PlayStatus = "Stopped"
		PlayBackMediaControl.Pause()
		Call frmAnimator.PositionFrameSlider(1) 'Adjust the position of the frame slider so it is correct
	End Sub
	Public Sub PlayButt()
		If FrameNum < 1 Then Exit Sub 'No frames have been captured, so take no action
		If WindowAtFront = "Live" Then Call Toggle() 'The live window is showing, so to flip to the playback window
		Call AudioPlayer_Play() 'Begin playing the soundtrack
		
		PlayStatus = "Playing"
		If PlayBackMediaPosition.CurrentPosition = PlayBackMediaPosition.Duration Then
			PlayBackMediaPosition.CurrentPosition = 0 'The current position was at the end of the .avi, so assume user wants a complete playback
			PlayBackMediaControl.Run() 'begin to play the media
		Else
			PlayBackMediaControl.Run() 'just begin to play the media
		End If
		
	End Sub
	Public Sub Forward1()
		If FrameNum < 1 Then Exit Sub 'No frames have been captured, so take no action
		If WindowAtFront = "Live" Then Call Toggle() 'The live window is showing, so to flip to the playback window
		Call AudioPlayer_Stop() 'Stop playing the soundtrack
		PlayBackMediaControl.Pause()
		PlayStatus = "Stopped"
		If PlayBackMediaPosition.CurrentPosition + (1 / FPS) > PlayBackMediaPosition.Duration Then 'Check if this will take it past end of the .avi
			PlayBackMediaPosition.CurrentPosition = PlayBackMediaPosition.Duration - (1 / FPS) 'Take it to the end of the file (less frame length then it will be at the start time of the last frame)
			Exit Sub
		Else
			PlayBackMediaPosition.CurrentPosition = PlayBackMediaPosition.CurrentPosition + (1 / FPS)
		End If
		Call frmAnimator.PositionFrameSlider(1) 'Adjust the position of the frame slider so it is correct
		
	End Sub
	Public Sub ToEnd()
		If FrameNum < 1 Then Exit Sub 'No frames have been captured, so take no action
		If WindowAtFront = "Live" Then Call Toggle() 'The live window is showing, so to flip to the playback window
		Call AudioPlayer_Stop() 'Stop playing the soundtrack
		
		PlayStatus = "Stopped"
		PlayBackMediaPosition.CurrentPosition = PlayBackMediaPosition.Duration '- (1 / FPS)      'Take it to the start of the last frame, which is the total duration, - frame length.
		Call frmAnimator.PositionFrameSlider(1) 'Adjust the position of the frame slider so it is correct
		
	End Sub
	Public Sub LoopButt() 'Moves back and plays the last section of the animation
		If FrameNum < 1 Then Exit Sub 'No frames have been captured, so take no action
		If WindowAtFront = "Live" Then Call Toggle() 'The live window is showing, so to flip to the playback window
		
		If PlayBackMediaPosition.Duration - (LoopLength / FPS) < 0 Then
			PlayBackMediaPosition.CurrentPosition = 0
			PlayBackMediaControl.Pause()
		Else
			PlayBackMediaPosition.CurrentPosition = PlayBackMediaPosition.Duration - (LoopLength / FPS)
		End If
		PlayBackMediaControl.Run()
		PlayStatus = "Looping"
		
	End Sub
	
	Public Sub TimeLapseUtility()
		If TimeLapseOnYesNo = False Then 'check if timelapse is running
			If MsgBox("Do you want to begin Time lapse mode?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Helium Frog Animator") = MsgBoxResult.Yes Then
				'UPGRADE_WARNING: Timer property tmrTimeLapse.Interval cannot have a value of 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="169ECF4A-1968-402D-B243-16603CC08604"'
				frmAnimator.tmrTimeLapse.Interval = TimeLapseDelay 'Set the interval time
				frmAnimator.tmrTimeLapse.Enabled = True 'Enable the timer
				TimeLapseOnYesNo = True 'Set flag on so main program can set captions etc.
			End If
		Else
			frmAnimator.tmrTimeLapse.Enabled = False
			TimeLapseOnYesNo = False 'Turn timelapse mode off
			'  MsgBox "Time Lapse Mode Ended", vbInformation + vbOKOnly, "Helium Frog Animator"                                    'Alert user that its switched off
		End If
	End Sub
	Public Sub SoundToggle() 'Switch sound on off
		If SoundTrackOnOff = True Then
			SoundTrackOnOff = False
			Call AudioPlayer_MinVolume() 'set sound to minimum
		Else
			SoundTrackOnOff = True
			Call AudioPlayer_MaxVolume() 'set sound to maximum
		End If
		Call frmMainForm.PrintMainCaption() 'Alter the main caption
	End Sub
	Public Sub XSheetToggle() 'Switch X sheet on / off
		
		If XsheetOnOff = True Then 'If the x sheet is showing then
			XsheetOnOff = False
			frmXSheet.Hide() 'Switch the X sheet off
			XSheetActiveYesNo = False
		Else
			XsheetOnOff = True
			ExposureTopPos = FrameNum
			Call frmXSheet.ReloadExposureSheet(ExposureTopPos)
			frmXSheet.Show() 'Switch the X sheet of
			XSheetActiveYesNo = True
		End If
		
	End Sub
End Module